"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = require("vue");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _classNames2 = _interopRequireDefault(require("../../_util/classNames"));

var _propsUtil = require("../../_util/props-util");

var _valueUtil = require("../utils/valueUtil");

var _context = require("../../table/context");

var _legacyUtil = require("../utils/legacyUtil");

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

function isRenderCell(data) {
  return data && (0, _typeof2.default)(data) === 'object' && !Array.isArray(data) && !(0, _propsUtil.isValidElement)(data);
}

var _default2 = (0, _vue.defineComponent)({
  name: 'Cell',
  props: ['prefixCls', 'record', 'index', 'dataIndex', 'customRender', 'component', 'colSpan', 'rowSpan', 'fixLeft', 'fixRight', 'firstFixLeft', 'lastFixLeft', 'firstFixRight', 'lastFixRight', 'appendNode', 'additionalProps', 'ellipsis', 'align', 'rowType', 'isSticky', 'column', 'cellType', 'transformCellText'],
  slots: ['appendNode'],
  setup: function setup(props, _ref) {
    var slots = _ref.slots;
    var contextSlots = (0, _context.useInjectSlots)();
    return function () {
      var _classNames;

      var _a, _b, _c, _d;

      var prefixCls = props.prefixCls,
          record = props.record,
          index = props.index,
          dataIndex = props.dataIndex,
          customRender = props.customRender,
          _props$component = props.component,
          Component = _props$component === void 0 ? 'td' : _props$component,
          colSpan = props.colSpan,
          rowSpan = props.rowSpan,
          fixLeft = props.fixLeft,
          fixRight = props.fixRight,
          firstFixLeft = props.firstFixLeft,
          lastFixLeft = props.lastFixLeft,
          firstFixRight = props.firstFixRight,
          lastFixRight = props.lastFixRight,
          _props$appendNode = props.appendNode,
          appendNode = _props$appendNode === void 0 ? (_a = slots.appendNode) === null || _a === void 0 ? void 0 : _a.call(slots) : _props$appendNode,
          _props$additionalProp = props.additionalProps,
          additionalProps = _props$additionalProp === void 0 ? {} : _props$additionalProp,
          ellipsis = props.ellipsis,
          align = props.align,
          rowType = props.rowType,
          isSticky = props.isSticky,
          _props$column = props.column,
          column = _props$column === void 0 ? {} : _props$column,
          cellType = props.cellType;
      var cellPrefixCls = "".concat(prefixCls, "-cell"); // ==================== Child Node ====================

      var cellProps;
      var childNode;
      var children = (_b = slots.default) === null || _b === void 0 ? void 0 : _b.call(slots);

      if ((0, _valueUtil.validateValue)(children) || cellType === 'header') {
        childNode = children;
      } else {
        var value = (0, _valueUtil.getPathValue)(record, dataIndex); // Customize render node

        childNode = value;

        if (customRender) {
          var renderData = customRender({
            text: value,
            value: value,
            record: record,
            index: index,
            column: column.__originColumn__
          });

          if (isRenderCell(renderData)) {
            childNode = renderData.children;
            cellProps = renderData.props;
          } else {
            childNode = renderData;
          }
        }

        if (!(_legacyUtil.INTERNAL_COL_DEFINE in column) && cellType === 'body' && contextSlots.value.bodyCell && !((_c = column.slots) === null || _c === void 0 ? void 0 : _c.customRender)) {
          var child = (0, _vue.renderSlot)(contextSlots.value, 'bodyCell', {
            text: value,
            value: value,
            record: record,
            index: index,
            column: column.__originColumn__
          }, function () {
            var fallback = childNode === undefined ? value : childNode;
            return [(0, _typeof2.default)(fallback) === 'object' && (0, _propsUtil.isValidElement)(fallback) || (0, _typeof2.default)(fallback) !== 'object' ? fallback : null];
          });
          childNode = (0, _propsUtil.flattenChildren)(child);
        }
        /** maybe we should @deprecated */


        if (props.transformCellText) {
          childNode = props.transformCellText({
            text: childNode,
            record: record,
            index: index,
            column: column.__originColumn__
          });
        }
      } // Not crash if final `childNode` is not validate ReactNode


      if ((0, _typeof2.default)(childNode) === 'object' && !Array.isArray(childNode) && !(0, _propsUtil.isValidElement)(childNode)) {
        childNode = null;
      }

      if (ellipsis && (lastFixLeft || firstFixRight)) {
        var _childNode = function () {
          return childNode;
        }();

        childNode = (0, _vue.createVNode)("span", {
          "class": "".concat(cellPrefixCls, "-content")
        }, [childNode]);
      }

      if (Array.isArray(childNode) && childNode.length === 1) {
        childNode = childNode[0];
      }

      var _e = cellProps || {},
          cellColSpan = _e.colSpan,
          cellRowSpan = _e.rowSpan,
          cellStyle = _e.style,
          cellClassName = _e.class,
          restCellProps = __rest(_e, ["colSpan", "rowSpan", "style", "class"]);

      var mergedColSpan = cellColSpan !== undefined ? cellColSpan : colSpan;
      var mergedRowSpan = cellRowSpan !== undefined ? cellRowSpan : rowSpan;

      if (mergedColSpan === 0 || mergedRowSpan === 0) {
        return null;
      } // ====================== Fixed =======================


      var fixedStyle = {};
      var isFixLeft = typeof fixLeft === 'number';
      var isFixRight = typeof fixRight === 'number';

      if (isFixLeft) {
        fixedStyle.position = 'sticky';
        fixedStyle.left = "".concat(fixLeft, "px");
      }

      if (isFixRight) {
        fixedStyle.position = 'sticky';
        fixedStyle.right = "".concat(fixRight, "px");
      } // ====================== Align =======================


      var alignStyle = {};

      if (align) {
        alignStyle.textAlign = align;
      } // ====================== Render ======================


      var title;
      var ellipsisConfig = ellipsis === true ? {
        showTitle: true
      } : ellipsis;

      if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === 'header')) {
        if (typeof childNode === 'string' || typeof childNode === 'number') {
          title = childNode.toString();
        } else if ((0, _vue.isVNode)(childNode) && typeof childNode.children === 'string') {
          title = childNode.children;
        }
      }

      var componentProps = (0, _extends2.default)((0, _extends2.default)((0, _extends2.default)({
        title: title
      }, restCellProps), additionalProps), {
        colSpan: mergedColSpan && mergedColSpan !== 1 ? mergedColSpan : null,
        rowSpan: mergedRowSpan && mergedRowSpan !== 1 ? mergedRowSpan : null,
        class: (0, _classNames2.default)(cellPrefixCls, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left"), isFixLeft), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-first"), firstFixLeft), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-last"), lastFixLeft), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right"), isFixRight), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-first"), firstFixRight), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-last"), lastFixRight), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-ellipsis"), ellipsis), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-with-append"), appendNode), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-sticky"), (isFixLeft || isFixRight) && isSticky), _classNames), additionalProps.class, cellClassName),
        style: (0, _extends2.default)((0, _extends2.default)((0, _extends2.default)((0, _extends2.default)({}, (0, _propsUtil.parseStyleText)(additionalProps.style)), alignStyle), fixedStyle), cellStyle)
      });
      return (0, _vue.createVNode)(Component, componentProps, {
        default: function _default() {
          return [appendNode, childNode, (_d = slots.dragHandle) === null || _d === void 0 ? void 0 : _d.call(slots)];
        }
      });
    };
  }
});

exports.default = _default2;